import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ButtonTest {

	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				ButtonFrame frame = new ButtonFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});

	}
	
}

class ButtonFrame extends JFrame {

	private JPanel buttonPanel;
	private JPanel buttonPanel1;

	public ButtonFrame() {
		setTitle("ButtonTest");
		setSize(300, 200);

		JButton yellowButton = new JButton("yellow");
		JButton blueButton = new JButton("blue");
		JButton redButton = new JButton("red");
		JButton changeLook = new JButton("change look and feel");

		buttonPanel = new JPanel();
		buttonPanel1 = new JPanel();

		buttonPanel.add(yellowButton);
		buttonPanel.add(redButton);
		buttonPanel.add(blueButton);

		buttonPanel1.add(changeLook);

		setLayout(new BorderLayout());
		add(buttonPanel, BorderLayout.NORTH);
		add(buttonPanel1, BorderLayout.CENTER);

		ColorAction yellowAction = new ColorAction(Color.yellow,"yellow");
		ColorAction blueAction = new ColorAction(Color.blue,"blue");
		ColorAction redAction = new ColorAction(Color.red,"red");

		LookAction lookAction = new LookAction();

		yellowButton.addActionListener(yellowAction);
		blueButton.addActionListener(blueAction);
		redButton.addActionListener(redAction);
		changeLook.addActionListener(lookAction);
		
		
		InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		imap.put(KeyStroke.getKeyStroke("ctrl Y"), "yellow");
		imap.put(KeyStroke.getKeyStroke("ctrl B"), "blue");
		imap.put(KeyStroke.getKeyStroke("ctrl R"), "red");
		
		ActionMap amap = buttonPanel.getActionMap();
		amap.put("yellow", yellowAction);
		amap.put("blue", blueAction);
		amap.put("red", redAction);

	}

	private class ColorAction extends AbstractAction {
		private Color backgroundColor;

		public ColorAction(Color c,String name) {
			putValue(Action.NAME, name+"1");
			putValue(Action.SMALL_ICON, null);
			putValue(Action.SHORT_DESCRIPTION, "change to "+name);
			backgroundColor = c;
		}

		public void actionPerformed(ActionEvent event) {
			buttonPanel.setBackground(backgroundColor);
			buttonPanel1.setBackground(backgroundColor);
		}
	}

	private class LookAction implements ActionListener {
		private UIManager.LookAndFeelInfo[] infos;
		private int key;

		public LookAction() {
			infos = UIManager.getInstalledLookAndFeels();
			for (UIManager.LookAndFeelInfo info : infos) {
				System.out.println(info.getName());
			}
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				UIManager.setLookAndFeel(infos[key].getClassName());
			} catch (ClassNotFoundException e1) {

				e1.printStackTrace();
			} catch (InstantiationException e1) {

				e1.printStackTrace();
			} catch (IllegalAccessException e1) {

				e1.printStackTrace();
			} catch (UnsupportedLookAndFeelException e1) {

				e1.printStackTrace();
			}
			SwingUtilities.updateComponentTreeUI(ButtonFrame.this);
			System.out.println(infos[key].getName());
			key = (key + 1) % infos.length;
		}

	}
}
